using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VMSEINKL
    {
        public DataRow Convert2MSEINKL(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryMSEINKL(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["MSEMANDANT"] = " ".PadRight(2 ); 
         dwReturn["MSEMAT"] = " ".PadRight(14 ); 
         dwReturn["MSEVAR"] = " ".PadRight(3 ); 
         dwReturn["MSEEKL"] = " ".PadRight(7 ); 
         dwReturn["MSEWERT1"] = " ".PadRight(15 ); 
         dwReturn["MSEWERT2"] = " ".PadRight(15 ); 
         dwReturn["MSEWERT3"] = " ".PadRight(15 ); 
         dwReturn["MSEWERT4"] = " ".PadRight(15 ); 
         dwReturn["MSEWERT5"] = " ".PadRight(15 ); 
         dwReturn["MSEWERT6"] = " ".PadRight(15 ); 
         dwReturn["MSEWERT7"] = " ".PadRight(15 ); 
         dwReturn["MSEWERT8"] = " ".PadRight(15 ); 
         dwReturn["MSEAENDZL"] = " ".PadRight(3 ); 
         dwReturn["MSEAENDBEN"] = " ".PadRight(8 ); 
         dwReturn["MSEAENDDAT"] = " ".PadRight(6 ); 
         dwReturn["MSENEUBEN"] = " ".PadRight(8 ); 
         dwReturn["MSENEUDAT"] = " ".PadRight(6 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapMSEINKL(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveMSEINKL(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryMSEINKL(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapMSEINKL(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertMSEINKL(dwLocal, ITXDBVERSION);
            }
            UpdateMSEINKL(dwLocal);
        }
        public void InsertMSEINKL(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO MSEINKL(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryMSEINKL(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM MSEINKL WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM MSEINKL  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "MSEINKL";
            Return = Result;
            return Return;
        }
        public void DeleteMSEINKL(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM MSEINKL   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateMSEINKL(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE MSEINKL SET "
                    +"MSEMANDANT= :MSEMANDANT,"
                    +"MSEMAT= :MSEMAT,"
                    +"MSEVAR= :MSEVAR,"
                    +"MSEEKL= :MSEEKL,"
                    +"MSEWERT1= :MSEWERT1,"
                    +"MSEWERT2= :MSEWERT2,"
                    +"MSEWERT3= :MSEWERT3,"
                    +"MSEWERT4= :MSEWERT4,"
                    +"MSEWERT5= :MSEWERT5,"
                    +"MSEWERT6= :MSEWERT6,"
                    +"MSEWERT7= :MSEWERT7,"
                    +"MSEWERT8= :MSEWERT8,"
                    +"MSEAENDZL= :MSEAENDZL,"
                    +"MSEAENDBEN= :MSEAENDBEN,"
                    +"MSEAENDDAT= :MSEAENDDAT,"
                    +"MSENEUBEN= :MSENEUBEN,"
                    +"MSENEUDAT= :MSENEUDAT,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("MSEMANDANT", OracleType.NVarChar).Value = dwData["MSEMANDANT"].ToString(); 
          or1.Parameters.Add("MSEMAT", OracleType.NVarChar).Value = dwData["MSEMAT"].ToString(); 
          or1.Parameters.Add("MSEVAR", OracleType.NVarChar).Value = dwData["MSEVAR"].ToString(); 
          or1.Parameters.Add("MSEEKL", OracleType.NVarChar).Value = dwData["MSEEKL"].ToString(); 
          or1.Parameters.Add("MSEWERT1", OracleType.NVarChar).Value = dwData["MSEWERT1"].ToString(); 
          or1.Parameters.Add("MSEWERT2", OracleType.NVarChar).Value = dwData["MSEWERT2"].ToString(); 
          or1.Parameters.Add("MSEWERT3", OracleType.NVarChar).Value = dwData["MSEWERT3"].ToString(); 
          or1.Parameters.Add("MSEWERT4", OracleType.NVarChar).Value = dwData["MSEWERT4"].ToString(); 
          or1.Parameters.Add("MSEWERT5", OracleType.NVarChar).Value = dwData["MSEWERT5"].ToString(); 
          or1.Parameters.Add("MSEWERT6", OracleType.NVarChar).Value = dwData["MSEWERT6"].ToString(); 
          or1.Parameters.Add("MSEWERT7", OracleType.NVarChar).Value = dwData["MSEWERT7"].ToString(); 
          or1.Parameters.Add("MSEWERT8", OracleType.NVarChar).Value = dwData["MSEWERT8"].ToString(); 
          or1.Parameters.Add("MSEAENDZL", OracleType.NVarChar).Value = dwData["MSEAENDZL"].ToString(); 
          or1.Parameters.Add("MSEAENDBEN", OracleType.NVarChar).Value = dwData["MSEAENDBEN"].ToString(); 
          or1.Parameters.Add("MSEAENDDAT", OracleType.NVarChar).Value = dwData["MSEAENDDAT"].ToString(); 
          or1.Parameters.Add("MSENEUBEN", OracleType.NVarChar).Value = dwData["MSENEUBEN"].ToString(); 
          or1.Parameters.Add("MSENEUDAT", OracleType.NVarChar).Value = dwData["MSENEUDAT"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
